﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows;

using System.ComponentModel;
using System.Collections.ObjectModel;
using WpfApp1.Models;
using WpfApp1.Services;
using DevExpress.Mvvm;

namespace WpfApp1.ViewModels
{
    public partial class MainViewModel : ViewModelBase,INotifyPropertyChanged
    {
        //用于生成内存文件目录树的原始数据
        public IList<Item> FileItemsData;

        /// <summary>
        /// 树形目录树,该类是针对直接读取目录树的方式来实现的
        /// </summary>
        private ObservableCollection<DirectoryRecord> directoryrecord { get; set; }
        public ObservableCollection<DirectoryRecord> DirectoryRecords
        {
            get
            {
                return directoryrecord;
            }
            set
            {
                if (this.directoryrecord != value)
                {
                    this.directoryrecord = value;
                    OnPropertyChanged("DirectoryRecords");
                }
            }
        }

        /// <summary>
        /// 树形目录数，用于内存类型的目录树的存在
        /// </summary>
        private ObservableCollection<Item> fileitems { get; set; }
        public ObservableCollection<Item> FileItems
        {
            get
            {
                return fileitems;
            }
            set
            {
                if (this.fileitems != value)
                {
                    this.fileitems = value;
                    OnPropertyChanged("FileItems");
                }
            }
        }

        /// <summary>
        /// 参数化数据的列表数据视图模型
        /// </summary>
        private ObservableCollection<ParametricDataDto> parametricDataDtos { get; set; }
        public ObservableCollection<ParametricDataDto> ParametricDataDtos
        {
            get { return parametricDataDtos; }
            set
            {
                if (this.parametricDataDtos != value)
                {
                    this.parametricDataDtos = value;
                    OnParameterChanged("ParametricDataDtos");
                }
            }
        }

        /// <summary>
        /// 测试用的模型，专门用于测试用
        /// </summary>
        private ObservableCollection<SourceDto> studentLists;
        public ObservableCollection<SourceDto> StudentLists
        {
            get
            {
                return studentLists;
            }
            set
            {
                if (this.studentLists != value)
                {
                    this.studentLists = value;
                    OnPropertyChanged("StudentLists");
                }
            }
        
        }

        /// <summary>
        /// 测试搜索的命令
        /// </summary>
        public DelegateCommand<object> SearchCommand { get; set; }
        /// <summary>
        /// 登陆命令的绑定
        /// </summary>
        public DelegateCommand LoginCommand { get; set; }
        /// <summary>
        /// 添加文件的命令
        /// </summary>
        public DelegateCommand<object> AddfileCommand { get; set; }
        /// <summary>
        /// 初始化文件夹的命令
        /// </summary>
        public DelegateCommand InitfolderCommand { get; set; }
        //同步文件参数的命令
        public DelegateCommand<object> HandleparaCommand { get; set; }

        public DelegateCommand SychronizeToserverCommand { get; set; }
        
        public DelegateCommand AddFolderServerCommand { get; set; }
        public DelegateCommand GetFileItemsFromSerCommand { get; set; }



        private string _name;
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        private string _newFileName;
        public string NewFileName
        {
            get { return _newFileName; }
            set
            {
                _newFileName = value;
                OnParameterChanged("NewFileName");
            }
        }

        //主页面中的用户名及密码
        private string userName;
        public string UserName
        {
            get { return userName; }
            set { SetProperty(ref userName, value, () => userName); }
        }

        private string password;
        public string Password
        {
            get { return password; }
            set { SetProperty(ref password, value, () => password); }
        }

        public void Testshow()
        {

        }
        

        public void Directory_Load()
        {
            DirectoryRecords = new ObservableCollection<DirectoryRecord>();

            List<string> paths = new List<string>()
            {
              @"E:\gitrepoprojects\FurioTest\ClientTest\Project1\"
            };

            foreach (var path in paths)
            {
                DirectoryRecords.Add(
                    new DirectoryRecord
                    {
                        Info = new DirectoryInfo(path)
                    }
                );
            }

        }

        /// <summary>
        /// 用于生成内存性质的目录树
        /// </summary>
        public void CreateItemTree()
        {
            if (FileItemsData == null)
            {
                FileItemsData = new List<Item>();
                var group1 = new Item() { Id = Guid.NewGuid().ToString(), DisplayText = "Root" };
                var item1 = new Item { Id = Guid.NewGuid().ToString(), ParentID = group1.Id, DisplayText = "Leaf1" };
                var item2 = new Item { Id = Guid.NewGuid().ToString(), ParentID = group1.Id, DisplayText = "Leaf2" };
                var item3 = new Item { Id = Guid.NewGuid().ToString(), ParentID = group1.Id, DisplayText = "Leaf3" };
                var item4 = new Item() { Id = Guid.NewGuid().ToString(), ParentID = item3.Id, DisplayText = "Leaf3:Leaf1" };
                var item5 = new Item() { Id = Guid.NewGuid().ToString(), ParentID = item3.Id, DisplayText = "Leaf3:Leaf2" };
                var item6 = new Item() { Id = Guid.NewGuid().ToString(), ParentID = item3.Id, DisplayText = "Leaf3:Leaf3" };
                FileItemsData.Add(group1);
                FileItemsData.Add(item1);
                FileItemsData.Add(item2);
                FileItemsData.Add(item3);
                FileItemsData.Add(item4);
                FileItemsData.Add(item5);
                FileItemsData.Add(item6);
            }
            FileItems =  TreeHelper.CreateTree<Item>(FileItemsData, item => item.Id, item => item.ParentID);
        }

        /// <summary>
        /// 公共属性变化的定义
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class Students : INotifyPropertyChanged
    {
        string _name;
        public int Id { get; set; }
        public string Name
        {
            get { return _name; }
            set { _name = value; OnPropertyChanged("Name"); }
        }
        public int Age { get; set; }
        protected internal virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
